#include <stdio.h>

#include "../normalization/normalized_datatypes.h"
#include "../normalization/normalization_globals.h"
#include "../normalization/print_utils.h"
#include "utils.h"
// #include "preprocessing_vars.h"
// #include <Judy.h>

// Pvoid_t original_premise_sizes = (Pvoid_t) NULL;

void preprocess() {
	int i, judy_return; // , conjunction_size;
	Word_t index;
	// int j;
	NormalizedAxiom* ax;
	NormalizedConcept* c;

	for (i = 0; i < normalized_tbox.axiom_count; ++i) {
		ax = normalized_tbox.axioms[i];
		switch (ax->type)  {
			case NORMALIZED_SUBCLASS_AX:
				c = ax->body.subclass_ax->lhs;
				switch (c->type) {
					case NORMALIZED_ATOMIC_CONCEPT:
						// if the lhs is atomic or existential, then premise size is 1
						// set_remaining_premise_size(c->description.atomic, ax, 1);
						// break;
					case NORMALIZED_EXISTENTIAL_RESTRICTION:
						add_containing_premise(c, ax);
						// add_to_singleton_premises(c, ax);
						break;
					case NORMALIZED_CONJUNCTION:
						// J1C(conjunction_size, ax->body.subclass_ax->lhs, 0, -1);
						// for the old version with conjuncts as dynamic array
						// for (j = 0; j < c->description.conj->size; ++j) {
						// 	switch (c->description.conj->conjuncts[j]->type) {
						// 		case NORMALIZED_ATOMIC_CONCEPT:
						// 		case NORMALIZED_EXISTENTIAL_RESTRICTION:
						// 			add_containing_premise(c->description.conj->conjuncts[j], ax);
						// 			break;
						// 		default:
						// 			fprintf(stderr, "unknown normalized axiom type, aborting!");
						// 			exit(-1);
						// 	}
						// }

						// J1C(conjunction_size, c->description.conj->conjuncts, 0, -1);
						// printf("conjunction size:%d\nprinting conjunction\n",conjunction_size);
						// print_normalized_concept(c);
						// printf("\nprinted conjunction\n");
						index = 0;
						J1F(judy_return, c->description.conj->conjuncts, index);
						if (judy_return == 0) {
							fprintf(stderr,"no conjunct in conjunction, aborting\n");
							exit(-1);
						}
						while (judy_return == 1) {
							// this switch can actually be omitted and the conjuncts can be 
							// directly added without checking type
							switch (((NormalizedConcept*) index)->type) {
								case NORMALIZED_ATOMIC_CONCEPT:
									// set_remaining_premise_size(((NormalizedConcept*) index)->description.atomic, ax, conjunction_size);
									// break;
								case NORMALIZED_EXISTENTIAL_RESTRICTION:
									add_containing_premise((NormalizedConcept*) index, ax);
									break;
								default:
									fprintf(stderr, "no other normalized concept type possible here, aborting!\n");
									exit(-1);
							}
							J1N(judy_return, c->description.conj->conjuncts, index);
						}
						break;
					default:
						fprintf(stderr, "unknown normalized axiom type, aborting!");
						exit(-1);
				}
				break;
      // TODO:
			case NORMALIZED_SUBROLE_AX:
				break;
			default:
				fprintf(stderr, "unknown normalized axiom type, aborting!");
				exit(-1);
		}
	}

}


